package com.example.dell.avtivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import DealWithMd5.DealData;
import NetRequest.SendRuest;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


/**
 * Created by dell on 2017/3/15.
 */

public class InPassword extends AppCompatActivity {

    public static final int REGISTER_FALSE = 8;
    public static final int REGISTER_TRUE = 9;

    private String token = "";
    private String TAG = "TAG";

    private EditText psd;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REGISTER_TRUE:
                    Toast.makeText(InPassword.this, "欢迎您，新用户", Toast.LENGTH_SHORT).show();
                    Log.d(TAG, "TOKEN is" + token);
                    break;
                case REGISTER_FALSE:
                    Toast.makeText(InPassword.this, "注册失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inpassword);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }

        psd = (EditText) findViewById(R.id.inpassword);
        Button btn = (Button) findViewById(R.id.psd_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String password = psd.getText().toString();
                if (TextUtils.isEmpty(password) || isPasswordValid(password)) {
                    Log.d(TAG, "psd" + password);
                    Toast.makeText(InPassword.this, "密码非法", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = getIntent();
                    String phone = intent.getStringExtra("phone");
                    reisterto(phone, DealData.getMd5(password));
                }
            }
        });

    }


    /*
验证密码长度 需补充
 */
    private boolean isPasswordValid(String password) {

        return password.length() < 4;
    }

    /*
    注册
     */
    private void reisterto(final String phone, String password) {
        SendRuest sr = new SendRuest(this);
        sr.SendRuest_Register(phone, password, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d(TAG, "registerto responsefail");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responsedata = response.body().string();
                try {
                    JSONObject jsonObject = new JSONObject(responsedata);
                    String status = jsonObject.getString("status");
                    if (!status.equals("success")) {
                        Message message = new Message();
                        message.what = REGISTER_FALSE;
                        handler.sendMessage(message);
                    } else {
                        token = jsonObject.getString("token");
                        Message message = new Message();
                        message.what = REGISTER_TRUE;
                        handler.sendMessage(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
